package com.uottawa.library.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uottawa.library.domain.User;
import com.uottawa.library.service.LoanService;
import com.uottawa.library.service.UserService;

/**
 * Servlet implementation class userServlet
 */
@WebServlet(name="/userServlet", urlPatterns={"/addUser.do", "/removeUser.do", "/ajaxNotification.do"}) 
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserService();
	private LoanService loanService = new LoanService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = request.getServletPath();
		
		if(url.contains("add"))
		{
			request.getRequestDispatcher("/user.jsp").forward(request, response);
			return;
		}
		
		if(url.contains("remove"))
		{
			String userId = request.getParameter("userId");
			if(userId != null)
			{
				String result = userService.deleteUser(Integer.parseInt(userId));
				request.setAttribute("result", result);
			}
			
			
			List<User> users = userService.getAllUser();
			request.setAttribute("users", users);
			
			request.getRequestDispatcher("/user.jsp").forward(request, response);
			
		}
		if(url.contains("ajaxNotification"))
		{
			
			User user = (User) request.getSession().getAttribute("user");
			String ajaxResponse ="";
			if(!loanService.checkNotification(user))
				ajaxResponse = "Dear "+ user.getUsername() + " , you have outstanding balance ...";
			else
				ajaxResponse ="Dear "+ user.getUsername() + " , you can borrow, renew and returen items ";
				
			PrintWriter writer = response.getWriter();
			writer.write(ajaxResponse);
			writer.flush();
		}
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = request.getServletPath();
		
		if(url.contains("add"))
		{
			String userName = request.getParameter("userName");
			String password = request.getParameter("password");
			
			if("".equals(userName) || "".equals(password)) {
				request.setAttribute("result", "all fields are required");
				request.getRequestDispatcher("/user.jsp").forward(request, response);
				return;
			}
			
			User user = new User();
			user.setUsername(userName);
			user.setPassword(password);
			user.setRole("user");
			
			String result = userService.addUser(user);
			request.setAttribute("result",result);
			request.getRequestDispatcher("/user.jsp").forward(request, response);
		}
	}

}
